import fse from 'fs-extra';
import path from 'path';
import { docDir } from './../util/settings';
import {MenuInfoDao} from "./MenuInfoDao";
// 将数据存至系统用户目录，防止用户误删程序
export const dbPath = path.join(docDir, 'data.sqlite3');
fse.ensureFileSync(dbPath);
var Conn = require('./../base/dbConn3.js');

// sqlites数据库地址
var file = dbPath;

// links the database
var links = null;

/**
 * { 构造函数 }
 */
function PicInfoDao() {
    console.log('likelike',links);
    console.log('PicInfoDao.constructor');
}

/**
 * Links a data base.
 */
PicInfoDao.prototype.linkDataBase = function() {
    console.dir(links)
    console.info(links.toString())
};

/**
 * 保存一个图片
 *
 * @param      {<type>}  params  The parameters
 * @return     {<type>}  { description_of_the_return_value }
 */
PicInfoDao.prototype.savePicInfo1 = function(params) {

    console.dir(params)
    var props = {}
    props.sql = 'INSERT INTO pic_info '
    props.sql += '(pic_name,pic_url,pic_size,pic_menu_id,create_time,catalog) '
    props.sql += ' VALUES'
    props.sql += ' (?,?,?,?,?,?);';

    props.value = params;
    console.log('savePicInfo+sql:::',props);

    try {
        links =new Conn.databaseCon(file);
        links.insertPlus(props);
    } catch (error) {
        console.error(error)
        throw error;
    } finally {
        links.closeConnect();
    }
};


PicInfoDao.prototype.batchInsert = function(params) {

    console.dir(params)
    var props = {}
    props.sql = 'INSERT INTO pic_info '
    props.sql += '(pic_name,pic_url,pic_size,pic_menu_id,create_time,catalog) '
    props.sql += ' VALUES'
    props.sql += ' (?,?,?,?,?,?);';

    console.log('savePicInfo+sql:::',props);

    try {
        links =new Conn.databaseCon(file);
        links.batchInsert(props,params);
    } catch (error) {
        console.error(error)
        throw error;
    } finally {
        links.closeConnect();
    }
};


/**
 * 根据一个或多个id删除一或多行
 *
 * @param      {<type>}  props   The properties
 * @return     {<type>}  { description_of_the_return_value }
 */
PicInfoDao.prototype.deleteByIds = function(props) {
    console.dir(props);
    var sql = "DELETE FROM pic_info WHERE ";
    sql += props.column
    sql += " IN "
    sql += " ("
    sql += props.value.join(", ");
    sql += ")"
    sql += " ;";
    console.log('deleteByIds+sql:::',sql);

    try {
        links =new Conn.databaseCon(file);
        links.deletes(sql);
    } catch (err) {
        console.error(err);
        return err;
    }
};

/**
 * { 根据id修改 }
 *
 * @param      {<type>}  user    The user
 * @return     {<type>}  { description_of_the_return_value }
 */
PicInfoDao.prototype.updatesById = function(picInfo) {
    if (picInfo == null || '' || undefined) {
        this.errorMsg = '参数不齐全!';
        console.error(this.errorMsg)
        throw this.errorMsg;
    }
    var sqlSentence = "UPDATE pic_info SET ";

    if (picInfo.picName != null || '' || undefined) {
        sqlSentence += " pic_name = ";
        sqlSentence += "'";
        sqlSentence += picInfo.picName;
        sqlSentence += "'";
        if (picInfo.picUrl != null || '' || undefined) {
            sqlSentence += ",";
        }
    }

    if (picInfo.picUrl != null || '' || undefined) {
        sqlSentence += "pic_url = ";
        sqlSentence += "'";
        sqlSentence += picInfo.picUrl;
        sqlSentence += "'";
        if (picInfo.picSize != null || '' || undefined) {
            sqlSentence += ",";
        }
    }

    if (picInfo.picSize != null || '' || undefined) {
        sqlSentence += "pic_size = ";
        sqlSentence += "'";
        sqlSentence += picInfo.picSize;
        sqlSentence += "'";
        if (picInfo.picMneuId != null || '' || undefined) {
            sqlSentence += ",";
        }
    }



    if (picInfo.picMneuId != null || '' || undefined) {
        sqlSentence += "pic_menu_id = ";
        sqlSentence += "'";
        sqlSentence += picInfo.picMneuId;
        sqlSentence += "'";
        if (picInfo.catalog != null || '' || undefined) {
            sqlSentence += ",";
        }
    }

    if (picInfo.catalog != null || '' || undefined) {
        sqlSentence += "catalog = ";
        sqlSentence += "'";
        sqlSentence += picInfo.catalog;
        sqlSentence += "' ";
    }

    if (picInfo.id == null || '' || undefined) {
        this.errorMsg = '参数[id]为空!';
        console.error(this.errorMsg)
        throw this.errorMsg;
    }
    sqlSentence += " WHERE id = ";
    sqlSentence += picInfo.id;
    sqlSentence += " ;";
    try {
        links =new Conn.databaseCon(file);
        links.executeCommand(sqlSentence);
    } catch (error) {
        console.error(error)
        throw error;
    } finally {
        links.closeConnect();
    }
};
/**
 * 获取图片列表
 *
 * @return     {<type>}  The user list.
 */
PicInfoDao.prototype.getPicInfoList = function() {
    var querySql = "SELECT ";
    querySql += "id "
    querySql += "FROM pic_info";
    querySql += ";";
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findAll(querySql).then(value => {
        console.log('成功态,接受resolved的结果...')
        console.dir(JSON.stringify(value));
        return value;
    }, value => {
        console.log('失败态,接受rejected的结果:')
        console.dir(JSON.stringify(value));
    })
    // links.closeConnect();
    return this.promiseObj;
};

/**
 * 通过id查询图片详情
 *
 * @param      {<type>}  field   The field
 * @return     {<type>}  The by field.
 */
PicInfoDao.prototype.getByField = function(field) {
    if (field == null || '' || undefined) {
        this.errorMsg = 'field对象为空!';
        console.error(this.errorMsg)
        return this.errorMsg;
    }

    var sql = "SELECT "
    sql += " id,pic_name,pic_url,pic_size,pic_menu_id,create_time "
    sql += " FROM pic_info "
    sql += " WHERE "
    sql += field.name
    sql += " = "

    sql += "'"
    sql += field.value
    sql += "'"
    sql += " ;";

    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(sql);
    // links.closeConnect();
    return this.promiseObj;
};
/**
 * 根据菜单id查找对应的菜单以及图片
 * @param field
 * @returns {*|string}
 */
PicInfoDao.prototype.getMenuAndPicByMenuId = function(field) {
    if (field == null || '' || undefined) {
        this.errorMsg = 'field对象为空!';
        console.error(this.errorMsg)
        return this.errorMsg;
    }

    var sql = "select * from ( select id,menu_name as name,'' as url,'' as size,0 as type,create_time as createTime from menu_info  where "
    sql += "parent_id = '"
    sql += field.id
    sql +="' "
    sql +=   "union "
    sql +=   "select id,pic_name as name ,pic_url as url,pic_size as size,1 as type,create_time as createTime from pic_info where "
    sql += "pic_menu_id = '"
    sql += field.id
    sql +="' "
    if(field.catalog != null && field.catalog != '' && field.catalog != undefined){
        sql += "and catalog = '"
        sql += field.catalog
        sql +="' "
    }
    sql +=  ") "
    if(field.queryName != null && field.queryName != '' && field.queryName != undefined){
        sql +=  "where name like"
        sql +="'%"
        sql +=field.queryName
        sql +="%'"
    }
    sql +=  "ORDER BY createTime desc "
    sql +=  "LIMIT "
    sql += field.pageNum
    sql +=","
    sql +=field.pageSize;
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(sql);
    console.log('getMenuAndPicByMenuIdgetMenuAndPicByMenuIdgetMenuAndPicByMenuId',this.promiseObj);
    return this.promiseObj;
};
/**
 * 获取所有的图片
 * @param field
 * @returns {*|string}
 */
PicInfoDao.prototype.getAllPic = function(field) {
    if (field == null || '' || undefined) {
        this.errorMsg = 'field对象为空!';
        console.error(this.errorMsg)
        return this.errorMsg;
    }
    var sql = "select id,pic_name as name ,pic_url as url,pic_size as size,1 as type,create_time as createTime from pic_info where " +
        " 1=1 "
    if(field.id != null && field.id != '' && field.id != undefined){
        sql += "and pic_menu_id in ("
        sql += field.id
        sql +=") "
    }

    if(field.catalog != null && field.catalog != '' && field.catalog != undefined){
        sql += "and catalog = '"
        sql += field.catalog
        sql +="' "
    }
    if(field.queryName != null && field.queryName != '' && field.queryName != undefined){
        sql +=  "and name like"
        sql +="'%"
        sql +=field.queryName
        sql +="%'"
    }
    sql +=  "ORDER BY createTime desc "
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(sql);
    return this.promiseObj;
};
/**
 * 获取所有的菜单
 * @param field
 * @returns {*|string}
 */
PicInfoDao.prototype.getAllMenu = function() {
    var sql = "select id,menu_name,parent_id,0 as type,create_time as createTime from menu_info "
    sql +=  " ORDER BY createTime desc "
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(sql);
    return this.promiseObj;
};

/**
 * 根据菜单id查找对应的菜单以及图片
 * @param field
 * @returns {*|string}
 */
PicInfoDao.prototype.getTotalNum = function(field) {
    if (field == null || '' || undefined) {
        this.errorMsg = 'field对象为空!';
        console.error(this.errorMsg)
        return this.errorMsg;
    }

    var sql = "select * from ( select id,menu_name as name,'' as url,'' as size,0 as type,create_time as createTime from menu_info  where "
    sql += "parent_id = '"
    sql += field.id
    sql +="' "
    sql +=   "union "
    sql +=   "select id,pic_name as name ,pic_url as url,pic_size as size,1 as type,create_time as createTime from pic_info where "
    sql += "pic_menu_id = '"
    sql += field.id
    sql +="' "
    sql +=  ") "
    if(field.queryName != null && field.queryName != '' && field.queryName != undefined){
        sql +=  "where name like"
        sql +="'%"
        sql +=field.queryName
        sql +="%'"
    }
    sql +=  "ORDER BY createTime desc ";
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(sql);
    return this.promiseObj;
};

PicInfoDao.prototype.getMenuByNoParam = function(params) {
    var querySql = "SELECT ";
    querySql += "id,menu_name,parent_id,create_time "
    querySql += "FROM menu_info where ";
    querySql += "id  "
    querySql += " NOT IN "
    querySql += " ("
    querySql += params.join(", ");
    querySql += ") "
    querySql += ";";
    links =new Conn.databaseCon(file);
    this.promiseObj = links.findByFieldValue(querySql);
    console.log('getMenuAndPicByMenuIdgetMenuAndPicByMenuIdgetMenuAndPicByMenuId',this.promiseObj);
    return this.promiseObj;
};
//导出模块给vue
export {
    PicInfoDao
}
